package com.example.demo.controller;


import com.example.demo.entity.Student;
import com.example.demo.entity.Teacher;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.mapper.TeacherMapper;
import com.example.demo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired(required = false)
    private TeacherMapper teacherMapper;
    @Autowired(required = false)
    private StudentMapper studentMapper;

    @RequestMapping("teacherPage")
    public ResultUtil teacherPage(HttpSession session, Integer page, Integer limit){
        page=(page-1)*limit;
        List<Teacher> teachers= teacherMapper.select(page, limit);
        Integer count = teacherMapper.selectTeacherCount();
        return new ResultUtil(0,"成功",count,teachers);
    }

    @RequestMapping("studentPage")
    public ResultUtil studentPage(HttpSession session, Integer page, Integer limit){
        page=(page-1)*limit;
        List<Student> students= studentMapper.select(page, limit);
        Integer count = studentMapper.selectStudentCount();
        return new ResultUtil(0,"成功",count,students);
    }

    @RequestMapping("addTeacher")
    public ResultUtil addTeacher(Teacher teacher){
        Integer integer = teacherMapper.insertTea(teacher);
        if (integer>0){
            //添加成功
            return new ResultUtil(200,"添加成功",null,null);
        }
        return new ResultUtil(500,"添加失败",null,null);
    }

    @RequestMapping("addStudent")
    public ResultUtil addStudent(Student student){
        Integer integer = studentMapper.insertStu(student);
        if (integer>0){
            //添加成功
            return new ResultUtil(200,"添加成功",null,null);
        }
        return new ResultUtil(500,"添加失败",null,null);
    }

    @RequestMapping("delStudent")
    public ResultUtil delStudent(Integer id){
        Integer integer = studentMapper.delById(id);
        if (integer>0){
            //添加成功
            return new ResultUtil(200,"删除成功",null,null);
        }
        return new ResultUtil(500,"删除失败",null,null);
    }

    @RequestMapping("delTeacher")
    public ResultUtil delTeacher(Integer id){
        Integer integer = teacherMapper.delById(id);
        if (integer>0){
            //添加成功
            return new ResultUtil(200,"删除成功",null,null);
        }
        return new ResultUtil(500,"删除失败",null,null);
    }

}
